package utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.bigkoo.svprogresshud.SVProgressHUD;

import java.io.File;

import log.LogManager;
import utils.common.ToastUtil;

/**
 * Created by TX on 2016/5/5.
 */


public class CacheManagerUtil {
    private static Context mContext;
    private static CacheManagerUtil cacheManagerUtil;
    private SVProgressHUD svProgressHUD;
    private int maxProgress;
    private static int count = 0;//缓存文件数
    private int delCount = 0;//已删除的缓存文件数

    public static CacheManagerUtil getCacheManagerUtil(Context context) {
        mContext = context;
        if (cacheManagerUtil == null) {
            synchronized (CacheManagerUtil.class) {
                if (cacheManagerUtil == null) {
                    cacheManagerUtil = new CacheManagerUtil();
                }
            }
        }
        return cacheManagerUtil;
    }

    /**
     * 遍历文件夹，获取缓存文件数
     */
    public int getFilesCount() {
        return getcacheFilesCount(new File(FileUtil.MAR_DIR));
    }

    private int getcacheFilesCount(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                File subDir = new File(dir, children[i]);
                if (subDir.list() != null && subDir.list().length == 0) {
                    continue;
                }
                count = getcacheFilesCount(subDir);
            }
        } else {
            count++;
        }

        return count;
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int delCount = (int)msg.obj;
            svProgressHUD.getProgressBar().setProgress(delCount * 100 / maxProgress);
            svProgressHUD.setText("正在清理中:" + delCount * 100 / maxProgress + "%");
            LogManager.getLogger().w("progress", delCount * 100 / maxProgress + "");
            if (delCount * 100 / maxProgress == 100) {
                svProgressHUD.dismiss();
                ToastUtil.showSuccessMsg("清理完成");
            }

        }
    };

    /**
     * 清理缓存文件，保留目录
     */
    public void clearCacheFiles() {
        maxProgress = 0;
        delCount = 0;
        count = 0;
        svProgressHUD = new SVProgressHUD(mContext);
        svProgressHUD.getProgressBar().setMax(100);
        svProgressHUD.getProgressBar().setProgress(0);
        maxProgress = getFilesCount();
        if (maxProgress == 0) {
            ToastUtil.showInfoMsg("没有缓存可清理");
        } else {
            svProgressHUD.showWithProgress("正在清理中:" + delCount * 100 / maxProgress + "%", SVProgressHUD.SVProgressHUDMaskType.Black);
            final File dir = new File(FileUtil.MAR_DIR);
            deleteDir(dir);
        }
    }

    /**
     * 递归删除文件
     *
     * @param dir
     */
    private void deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                File subDir = new File(dir, children[i]);
                if (subDir.list() != null && subDir.list().length == 0) {
                    continue;
                }
                deleteDir(subDir);
            }
        } else {
            deleteFile(dir);
        }
    }


    /**
     * 删除文件
     *
     * @param file
     * @return
     */
    private int deleteFile(File file) {
        if (file.delete()) {
            delCount++;
            Message message = Message.obtain();
            message.obj = delCount;
            mHandler.sendMessageDelayed(message, 500);
        }
        return delCount;
    }
}
